package com.jqb.jsplab.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jqb.jsplab.dao.factory.DaoFactory;
import com.jqb.jsplab.dao.proxy.TestDaoProxy;

public class FileOperateServlet extends HttpServlet {

	private static final long serialVersionUID = 6115978388603905852L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String fileoperat = null;
		fileoperat = request.getParameter("fileoperate");
		TestDaoProxy testdaoproxy = null;
		try {
			testdaoproxy = DaoFactory.getInstanceTestDaoProxy();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if ("upload".equals(fileoperat)) {
			if (testdaoproxy.fileUpload(request, response)) {
				request.setAttribute("msg", "upload successed...");
				request.getRequestDispatcher("/jspdemo/fileupload.jsp").forward(request, response);
			} else {
				request.setAttribute("msg", "upload faied...");
				request.getRequestDispatcher("/jspdemo/fileupload.jsp").forward(request, response);
			}
		}
		if ("smartupload".equals(fileoperat)) {
			if (testdaoproxy.smartFileUpload(this, request, response)) {
				request.setAttribute("msg", "smart upload successed...");
				request.getRequestDispatcher("/jspdemo/fileuploadimage.jsp").forward(request, response);
			} else {
				request.setAttribute("msg", "smart upload failed...");
				request.getRequestDispatcher("/jspdemo/fileuploadimage.jsp").forward(request, response);
			}
		}
		if ("smartdownload".equals(fileoperat)) {
			String filename = request.getParameter("filename");
			System.out.println(filename);
			if (testdaoproxy.smartFileDownload(this, request, response,filename)) {
				System.out.println("downloading...");
			} else {
				System.out.println("download failed...");
			}
		}
		if ("multifileupload".equals(fileoperat)) {
			if (testdaoproxy.multiFileUpload(request, response)) {
				request.setAttribute("msg", "smart upload successed...");
				request.getRequestDispatcher("/jspdemo/fileuploadmulti.jsp").forward(request, response);
			} else {
				request.setAttribute("msg", "smart upload failed...");
				request.getRequestDispatcher("/jspdemo/fileuploadmulti.jsp").forward(request, response);
			}
		}
	}
}
